var DataTypes = require('sequelize').DataTypes
var _sys_dept = require('./sys_dept')
var _sys_menu = require('./sys_menu')
var _sys_post = require('./sys_post')
var _sys_role = require('./sys_role')
var _sys_role_dept = require('./sys_role_dept')
var _sys_role_menu = require('./sys_role_menu')
var _sys_user = require('./sys_user')
var _sys_user_post = require('./sys_user_post')
var _sys_user_role = require('./sys_user_role')

function initModels(sequelize) {
  var sys_dept = _sys_dept(sequelize, DataTypes)
  var sys_menu = _sys_menu(sequelize, DataTypes)
  var sys_post = _sys_post(sequelize, DataTypes)
  var sys_role = _sys_role(sequelize, DataTypes)
  var sys_role_dept = _sys_role_dept(sequelize, DataTypes, {
    sys_dept,
    sys_role
  })
  var sys_role_menu = _sys_role_menu(sequelize, DataTypes, {
    sys_menu,
    sys_role
  })

  sys_role.belongsToMany(sys_dept, { through: sys_role_dept, foreignKey: 'role_id', constraints: false })
  sys_dept.belongsToMany(sys_role, { through: sys_role_dept, foreignKey: 'dept_id', constraints: false })

  sys_role.belongsToMany(sys_menu, { through: sys_role_menu, foreignKey: 'role_id', constraints: false })
  sys_menu.belongsToMany(sys_role, { through: sys_role_menu, foreignKey: 'menu_id', constraints: false })

  var sys_user = _sys_user(sequelize, DataTypes)
  var sys_user_post = _sys_user_post(sequelize, DataTypes, {
    sys_post,
    sys_user
  })
  var sys_user_role = _sys_user_role(sequelize, DataTypes, {
    sys_role,
    sys_user
  })

  sys_user.belongsToMany(sys_post, { through: sys_user_post, foreignKey: 'user_id', constraints: false })
  sys_post.belongsToMany(sys_user, { through: sys_user_post, foreignKey: 'post_id', constraints: false })

  sys_user.belongsToMany(sys_role, { through: sys_user_role, foreignKey: 'user_id', constraints: false })
  sys_role.belongsToMany(sys_user, { through: sys_user_role, foreignKey: 'role_id', constraints: false })

  sys_user.belongsTo(sys_dept, { foreignKey: 'dept_id', constraints: false, as: 'dept' })

  return {
    sys_dept,
    sys_menu,
    sys_post,
    sys_role,
    sys_role_dept,
    sys_role_menu,
    sys_user,
    sys_user_post,
    sys_user_role
  }
}
module.exports = initModels
module.exports.initModels = initModels
module.exports.default = initModels
